<?php

namespace app\subscribe\energy;

use think\Event;
use app\common\repositories\room\RoomLogRepository;
use app\common\repositories\energy\EnergyBillRepository;
use app\common\repositories\energy\EnergyMeterRepository;
use app\common\repositories\energy\EnergyMeterLogRepository;
use app\common\repositories\energy\EnergyMeterDayRepository;
use app\common\repositories\energy\EnergyBalanceLogRepository;
use app\common\repositories\energy\EnergyOperateLogRepository;
use app\common\repositories\property\basic\PropertyLogRepository;

class EnergyMeterSubscribe
{

    /**
     * 监听电表创建
     * @param $data
     * @return mixed|void
     *
     * @author Amos
     * 2024-04-10 16:13
     */
    public function onCreate($data)
    {
        $meterInfo = $data['meterInfo'];
        $userId = $data['userId'] ?? '0';

        /** @var EnergyMeterLogRepository $meterLogRepository */
        $meterLogRepository = app()->make(EnergyMeterLogRepository::class);
        $logData['remark'] = '创建' . ($data['remark'] ?? '');
        $logData['user_id'] = $userId;
        $logData['energy_id'] = $meterInfo['id'];
        $meterLogRepository->addInfo($meterInfo['property_id'], $logData, $userId);

        ## 判断是否是智能表
        if ($meterInfo['wit_type'] == 1) {
            ## 召测状态信息
            /** @var EnergyOperateLogRepository $operateLogRepository */
            $operateLogRepository = app()->make(EnergyOperateLogRepository::class);
            $param['user_id'] = $userId;
            $param['remark'] = '创建后状态更新';
            $param['energy_id'] = $meterInfo['id'];
            $param['operate_type'] = 'open_reading';
            $param['energy_type'] = $meterInfo['energy_type'];
            return $operateLogRepository->addInfo($meterInfo['property_id'], $param, $userId);
        }
        if ((int)$meterInfo['property_id'] > 0) {
            $roomId = (int)$meterInfo['room_id'];
            if ($roomId > 0) {
                $roomRemarl = '，绑定房间ID: ' . $roomId;
            } else {
                $roomRemarl = '';
            }
            /** @var PropertyLogRepository $propertyLogRepository */
            $propertyLogRepository = app()->make(PropertyLogRepository::class);
            $propertyLogData['user_id'] = $userId;
            $propertyLogData['property_id'] = $meterInfo['property_id'];
            $propertyLogData['remark'] = $meterInfo['energy_name'] . ' 电表创建' . $roomRemarl;
            $propertyLogRepository->addData($meterInfo['property_id'], $propertyLogData, $userId);
        }
    }


    /**
     * 监听电表编辑
     * @param $data
     */
    public function onRedact($data)
    {
        $nowMeterInfo = $data['meterInfo'];
        $userId = $data['userId'] ?? '0';

        /** @var EnergyMeterRepository $energyMeterRepository */
        $energyMeterRepository = app()->make(EnergyMeterRepository::class);

        $newMeterInfo = $energyMeterRepository->get($nowMeterInfo['id']);

        /** @var EnergyMeterLogRepository $meterLogRepository */
        $meterLogRepository = app()->make(EnergyMeterLogRepository::class);
        $logData['remark'] = '编辑';
        if ($nowMeterInfo['plan_id'] != $newMeterInfo['plan_id']) {
            $logData['remark'] .= '，收费方案：' . $nowMeterInfo['plan_id'] . '&' . $newMeterInfo['plan_id'];
        }
        if ($nowMeterInfo['wit_type'] != $newMeterInfo['wit_type']) {
            $logData['remark'] .= '，表计类型：' . $nowMeterInfo['wit_type'] . '&' . $newMeterInfo['wit_type'];
        }
        if ($nowMeterInfo['energy_no'] != $newMeterInfo['energy_no']) {
            $logData['remark'] .= '，设备编号：' . $nowMeterInfo['energy_no'] . '&' . $newMeterInfo['energy_no'];
        }
        if ($nowMeterInfo['is_public'] != $newMeterInfo['is_public']) {
            $logData['remark'] .= '，安装属性：' . $nowMeterInfo['is_public'] . '&' . $newMeterInfo['is_public'];
        }
        if ($nowMeterInfo['energy_name'] != $newMeterInfo['energy_name']) {
            $logData['remark'] .= '，安装地址：' . $nowMeterInfo['energy_name'] . '&' . $newMeterInfo['energy_name'];
        }
        if ($nowMeterInfo['beilv'] != $newMeterInfo['beilv']) {
            $logData['remark'] .= '，计量倍率：' . $nowMeterInfo['beilv'] . '&' . $newMeterInfo['beilv'];
        }
        $logData['user_id'] = $userId;
        $logData['energy_id'] = $nowMeterInfo['id'];
        $meterLogRepository->addInfo($nowMeterInfo['property_id'], $logData, $userId);
    }


    /**
     * 监听设备删除
     * @param $data
     */
    public function onDelete($data)
    {
        $meterInfo = $data['meterInfo'];
        $userId = $data['userId'] ?? '0';


        /** @var EnergyMeterRepository $energyMeterRepository */
        $energyMeterRepository = app()->make(EnergyMeterRepository::class);
        $energuTypeData = $energyMeterRepository::ENERGY_TYPE;

        if ((int)$meterInfo['property_id'] > 0) {
            $roomId = (int)$meterInfo['room_id'];
            /** @var PropertyLogRepository $propertyLogRepository */
            $propertyLogRepository = app()->make(PropertyLogRepository::class);
            $propertyLogData['user_id'] = $userId;
            $propertyLogData['property_id'] = $meterInfo['property_id'];
            $propertyLogData['remark'] = ($energuTypeData[$meterInfo['energy_type']] ?? '') . '设备编号：' . $meterInfo['energy_no'] . '删除 安装地址' . $meterInfo['energy_name'];
            $propertyLogRepository->addData($meterInfo['items_id'], $propertyLogData, $userId);
        }
        $roomId = (int)$meterInfo['room_id'];
        if ($roomId > 0) {
            /** @var RoomLogRepository $roomLogRepository */
            $roomLogRepository = app()->make(RoomLogRepository::class);
            $roomLogData['room_id'] = $roomId;
            $roomLogData['property_id'] = $meterInfo['property_id'];
            $roomLogData['remark'] = ($energuTypeData[$meterInfo['energy_type']] ?? '') . '设备编号：' . $meterInfo['energy_no'] . '删除  安装地址' . $meterInfo['energy_name'];
            $roomLogRepository->addData($meterInfo['property_id'], $roomLogData, $userId);
        }

        # 删除日志记录
        /** @var EnergyMeterLogRepository $meterLogRepository */
        $meterLogRepository = app()->make(EnergyMeterLogRepository::class);
        $meterLogRepository->removeMeterId($meterInfo['id']);

        # 删除操作记录
        /** @var EnergyOperateLogRepository $operateLogRepository */
        $operateLogRepository = app()->make(EnergyOperateLogRepository::class);
        $operateLogRepository->removeMeterId($meterInfo['id']);

        # 删除日用量记录
        /** @var EnergyMeterDayRepository $energyMeterDayRepository */
        $energyMeterDayRepository = app()->make(EnergyMeterDayRepository::class);
        $energyMeterDayRepository->removeMeterId($meterInfo['id']);

        # 删除储值记录
        /** @var EnergyBalanceLogRepository $energyBalanceLogRepository */
        $energyBalanceLogRepository = app()->make(EnergyBalanceLogRepository::class);
        $energyBalanceLogRepository->removeMeterId($meterInfo['id']);

        # 删除账单记录
        /** @var EnergyBillRepository $energyBillRepository */
        $energyBillRepository = app()->make(EnergyBillRepository::class);
        $energyBillRepository->removeMeterId($meterInfo['id']);
    }


    /**
     * 监听抄读
     * @param $data
     */
    public function onAutoRead($data)
    {
        $meterInfo = $data['meterInfo'];
        $userId = $data['userId'] ?? '0';


        /** @var EnergyMeterLogRepository $meterLogRepository */
        $meterLogRepository = app()->make(EnergyMeterLogRepository::class);
        if ($data['remark']) {
            $logData['remark'] = $data['remark'];
        }
        $logData['user_id'] = $userId;
        $logData['energy_id'] = $meterInfo['id'];
        $meterLogRepository->addInfo($meterInfo['property_id'], $logData, $userId);

        if ((int)$meterInfo['property_id'] > 0) {
            $roomId = (int)$meterInfo['room_id'];
            if ($roomId > 0) {
                $roomRemarl = '，房间ID: ' . $roomId;
            } else {
                $roomRemarl = '';
            }
            /** @var PropertyLogRepository $propertyLogRepository */
            $propertyLogRepository = app()->make(PropertyLogRepository::class);
            $propertyLogData['user_id'] = $userId;
            $propertyLogData['property_id'] = $meterInfo['property_id'];
            $propertyLogData['remark'] = $meterInfo['energy_name'] . ' 电表用量抄读' . $roomRemarl;
            $propertyLogRepository->addData($meterInfo['property_id'], $propertyLogData, $userId);
        }
    }


    /**
     * 监听电表人工抄读
     * @param $data
     */
    public function onHandReading($data)
    {
        $meterInfo = $data['meterInfo'];
        $userId = $data['userId'] ?? '0';

        /** @var EnergyMeterLogRepository $meterLogRepository */
        $meterLogRepository = app()->make(EnergyMeterLogRepository::class);
        $logData['remark'] = '日用量抄读' . ($data['remark'] ?? '');
        $logData['user_id'] = $userId;
        $logData['energy_id'] = $meterInfo['id'];
        $meterLogRepository->addInfo($meterInfo['property_id'], $logData, $userId);

        if ((int)$meterInfo['property_id'] > 0) {
            /** @var PropertyLogRepository $propertyLogRepository */
            $propertyLogRepository = app()->make(PropertyLogRepository::class);
            $propertyLogData['user_id'] = $userId;
            $propertyLogData['property_id'] = $meterInfo['property_id'];
            $propertyLogData['remark'] = $meterInfo['energy_name'] . '电表日用量抄读';
            $propertyLogRepository->addData($meterInfo['property_id'], $propertyLogData, $userId);
        }
    }

    /**
     * 监听更新
     * @param $data
     */
    public function onUpdateOnOff($data)
    {
        $meterInfo = $data['meterInfo'];
        $userId = $data['userId'] ?? '0';

        /** @var EnergyMeterLogRepository $meterLogRepository */
        $meterLogRepository = app()->make(EnergyMeterLogRepository::class);
        if ($data['remark']) {
            $logData['remark'] = $data['remark'];
        }
        $logData['user_id'] = $userId;
        $logData['energy_id'] = $meterInfo['id'];
        $meterLogRepository->addInfo($meterInfo['property_id'], $logData, $userId);

        if ((int)$meterInfo['property_id'] > 0) {
            $roomId = (int)$meterInfo['room_id'];
            if ($roomId > 0) {
                $roomRemarl = '，房间ID: ' . $roomId;
            } else {
                $roomRemarl = '';
            }
            /** @var PropertyLogRepository $propertyLogRepository */
            $propertyLogRepository = app()->make(PropertyLogRepository::class);
            $propertyLogData['user_id'] = $userId;
            $propertyLogData['property_id'] = $meterInfo['property_id'];
            $propertyLogData['remark'] = $meterInfo['energy_name'] . ' 电表状态更新' . $roomRemarl;
            $propertyLogRepository->addData($meterInfo['property_id'], $propertyLogData, $userId);
        }
    }

    /**
     * 监听拉闸
     * @param $data
     */
    public function onOffEnergyMeter($data)
    {
        $meterInfo = $data['meterInfo'];
        $userId = $data['userId'] ?? '0';

        /** @var EnergyMeterLogRepository $meterLogRepository */
        $meterLogRepository = app()->make(EnergyMeterLogRepository::class);
        if ($data['remark']) {
            $logData['remark'] = $data['remark'];
        }
        $logData['user_id'] = $userId;
        $logData['energy_id'] = $meterInfo['id'];
        $meterLogRepository->addInfo($meterInfo['property_id'], $logData, $userId);

        if ((int)$meterInfo['property_id'] > 0) {
            $roomId = (int)$meterInfo['room_id'];
            if ($roomId > 0) {
                $roomRemarl = '，房间ID: ' . $roomId;
            } else {
                $roomRemarl = '';
            }
            /** @var PropertyLogRepository $propertyLogRepository */
            $propertyLogRepository = app()->make(PropertyLogRepository::class);
            $propertyLogData['user_id'] = $userId;
            $propertyLogData['property_id'] = $meterInfo['property_id'];
            $propertyLogData['remark'] = $meterInfo['energy_name'] . ' 电表拉闸' . $roomRemarl;
            $propertyLogRepository->addData($meterInfo['property_id'], $propertyLogData, $userId);
        }
    }

    /**
     * 监听合闸
     * @param $data
     */
    public function onOnEnergyMeter($data)
    {
        $meterInfo = $data['meterInfo'];
        $userId = $data['userId'] ?? '0';

        /** @var EnergyMeterLogRepository $meterLogRepository */
        $meterLogRepository = app()->make(EnergyMeterLogRepository::class);
        if ($data['remark']) {
            $logData['remark'] = $data['remark'];
        }
        $logData['user_id'] = $userId;
        $logData['energy_id'] = $meterInfo['id'];
        $meterLogRepository->addInfo($meterInfo['property_id'], $logData, $userId);

        if ((int)$meterInfo['property_id'] > 0) {
            $roomId = (int)$meterInfo['room_id'];
            if ($roomId > 0) {
                $roomRemarl = '，房间ID: ' . $roomId;
            } else {
                $roomRemarl = '';
            }
            /** @var PropertyLogRepository $propertyLogRepository */
            $propertyLogRepository = app()->make(PropertyLogRepository::class);
            $propertyLogData['user_id'] = $userId;
            $propertyLogData['property_id'] = $meterInfo['property_id'];
            $propertyLogData['remark'] = $meterInfo['energy_name'] . ' 电表合闸' . $roomRemarl;
            $propertyLogRepository->addData($meterInfo['property_id'], $propertyLogData, $userId);
        }
    }

    /**
     * 监听能源设备绑定房间
     * @param $data
     */
    public function onBindRoom($data)
    {
        $roomId = $data['roomId'];
        $meterInfo = $data['meterInfo'];
        $userId = $data['userId'] ?? '0';
        $remark = $data['remark'] ?? '';

        /** @var EnergyMeterLogRepository $meterLogRepository */
        $meterLogRepository = app()->make(EnergyMeterLogRepository::class);
        if ($meterInfo['energy_type'] == 11) {
            $remark .= '电表';
        }
        if ($meterInfo['energy_type'] == 12) {
            $remark .= '冷水表';
        }
        if ($meterInfo['energy_type'] == 13) {
            $remark .= '热水表';
        }
        if ($meterInfo['energy_type'] == 14) {
            $remark .= '燃气表';
        }
        $logData['remark'] = $remark . '绑定房间ID：' . $roomId;
        $logData['user_id'] = $userId;
        $logData['energy_id'] = $meterInfo['id'];
        $meterLogRepository->addInfo($meterInfo['items_id'], $logData, $userId);

        if ($meterInfo['property_id'] > 0) {
            /** @var PropertyLogRepository $propertyLogRepository */
            $propertyLogRepository = app()->make(PropertyLogRepository::class);
            $propertyLogData['property_id'] = $meterInfo['property_id'];
            $propertyLogData['remark'] = $remark . '设备编号：' . $meterInfo['energy_no'] . '绑定';
            $propertyLogRepository->addData($meterInfo['property_id'], $propertyLogData, $userId);
        }

        if ($roomId > 0) {
            /** @var RoomLogRepository $roomLogRepository */
            $roomLogRepository = app()->make(RoomLogRepository::class);
            $roomLogData['room_id'] = $roomId;
            $roomLogData['property_id'] = $meterInfo['property_id'];
            $roomLogData['remark'] = $remark . '设备编号：' . $meterInfo['energy_no'] . '绑定';
            $roomLogRepository->addData($meterInfo['property_id'], $roomLogData, $userId);
        }
    }


    /**
     * 监听电表解除房间绑定
     * @param $data
     * @return void
     *
     * @author Amos
     * 2024-05-18 23:13
     */
    public function onUnbindRoom($data)
    {
        $meterInfo = $data['meterInfo'];
        $userId = $data['userId'] ?? '0';
        $remark = $data['remark'] ?? '';
        $roomId = $data['roomId'] ?? '';

        /** @var EnergyMeterLogRepository $meterLogRepository */
        $meterLogRepository = app()->make(EnergyMeterLogRepository::class);
        if ($meterInfo['energy_type'] == 11) {
            $remark .= '电表';
        }
        if ($meterInfo['energy_type'] == 12) {
            $remark .= '冷水表';
        }
        if ($meterInfo['energy_type'] == 13) {
            $remark .= '热水表';
        }
        if ($meterInfo['energy_type'] == 14) {
            $remark .= '燃气表';
        }
        $logData['remark'] = $remark . '解除房间ID：' . $roomId;
        $logData['user_id'] = $userId;
        $logData['energy_id'] = $meterInfo['id'];
        $meterLogRepository->addInfo($meterInfo['property_id'], $logData, $userId);

        if ($meterInfo['property_id'] > 0) {
            /** @var PropertyLogRepository $propertyLogRepository */
            $propertyLogRepository = app()->make(PropertyLogRepository::class);
            $propertyLogData['property_id'] = $meterInfo['property_id'];
            $propertyLogData['remark'] = $remark . '设备编号：' . $meterInfo['energy_no'] . '解除';
            $propertyLogRepository->addData($meterInfo['property_id'], $propertyLogData, $userId);
        }

        if ($roomId > 0) {
            /** @var RoomLogRepository $roomLogRepository */
            $roomLogRepository = app()->make(RoomLogRepository::class);
            $roomLogData['room_id'] = $roomId;
            $roomLogData['property_id'] = $meterInfo['property_id'];
            $roomLogData['remark'] = $remark . '设备编号：' . $meterInfo['energy_no'] . '解除';
            $roomLogRepository->addData($meterInfo['property_id'], $roomLogData, $userId);
        }
    }

    /**
     *  设备倍率更新
     * @param $data
     * @return void
     *
     * @author Amos
     * 2024-05-18 23:13
     */
    public function onModifyBeilv($data)
    {
        $meterInfo = $data['meterInfo'];
        $userId = $data['userId'] ?? '0';
        $remark = $data['remark'] ?? '';

        /** @var EnergyMeterLogRepository $meterLogRepository */
        $meterLogRepository = app()->make(EnergyMeterLogRepository::class);
        $logData['remark'] = $remark;
        $logData['user_id'] = $userId;
        $logData['energy_id'] = $meterInfo['id'];
        $meterLogRepository->addInfo($meterInfo['property_id'], $logData, $userId);

        if ($meterInfo['property_id'] > 0) {
            /** @var PropertyLogRepository $propertyLogRepository */
            $propertyLogRepository = app()->make(PropertyLogRepository::class);
            $propertyLogData['remark'] = $remark;
            $propertyLogData['property_id'] = $meterInfo['property_id'];
            $propertyLogRepository->addData($meterInfo['property_id'], $propertyLogData, $userId);
        }

    }

    /**
     * 设备权益转让
     * @param $data
     * @return void
     *
     * @author Amos
     * 2024-05-18 23:13\
     */
    public function onMakeOver($data)
    {
        $meterInfo = $data['meterInfo'];
        $userId = $data['userId'] ?? '0';
        $remark = $data['remark'] ?? '';

        /** @var EnergyMeterLogRepository $meterLogRepository */
        $meterLogRepository = app()->make(EnergyMeterLogRepository::class);
        $logData['remark'] = $remark;
        $logData['user_id'] = $userId;
        $logData['energy_id'] = $meterInfo['id'];
        $meterLogRepository->addInfo($meterInfo['property_id'], $logData, $userId);

        if ($meterInfo['property_id'] > 0) {
            /** @var PropertyLogRepository $propertyLogRepository */
            $propertyLogRepository = app()->make(PropertyLogRepository::class);
            $propertyLogData['remark'] = $remark;
            $propertyLogData['property_id'] = $meterInfo['property_id'];
            $propertyLogRepository->addData($meterInfo['property_id'], $propertyLogData, $userId);
        }

    }


    public function subscribe(Event $event)
    {
        $event->listen('energy.meter.create', [$this, 'onCreate']); ## 电表添加
        $event->listen('energy.meter.redact', [$this, 'onRedact']); ## 电表编辑
        $event->listen('energy.meter.delete', [$this, 'onDelete']);## 电表删除
        $event->listen('energy.meter.bindRoom', [$this, 'onBindRoom']);## 房间关联
        $event->listen('energy.meter.unbindRoom', [$this, 'onUnbindRoom']);## 关联解除
        $event->listen('energy.meter.autoRead', [$this, 'onAutoRead']); ## 电表抄读
        $event->listen('energy.meter.handReading', [$this, 'onHandReading']); ##人工抄读
        $event->listen('energy.meter.updateOnOff', [$this, 'onUpdateOnOff']);## 更新状态
        $event->listen('energy.meter.offEnergyMeter', [$this, 'onOffEnergyMeter']);## 拉闸
        $event->listen('energy.meter.onEnergyMeter', [$this, 'onOnEnergyMeter']);## 合闸
        $event->listen('energy.meter.modifyBeilv', [$this, 'onModifyBeilv']); ## 倍率更新
        $event->listen('energy.meter.makeOver', [$this, 'onMakeOver']); ## 权益转让
    }
}